package com.zzf.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzf.eduservice.client.VodClient;
import com.zzf.eduservice.entity.EduVideo;
import com.zzf.eduservice.mapper.EduVideoMapper;
import com.zzf.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-04-05
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {


    @Autowired
    private VodClient vodClient;

    //根据课程ID删除小节
    //todo 删除小节,删除对应的视频文件
    @Override
    public void removeVideoByCourseId(String courseId) {
        //1:根据课程id查询出所有的视频id
        QueryWrapper<EduVideo> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("course_id", courseId);//设置查询条件
        videoQueryWrapper.select("video_source_id");//设置要查询的值
        List<EduVideo> eduVideos = baseMapper.selectList(videoQueryWrapper);

        ArrayList<String> videoIds = new ArrayList<>();
        for (EduVideo eduVideo :
                eduVideos) {
            if (!StringUtils.isEmpty(eduVideo.getVideoSourceId())) {
                videoIds.add(eduVideo.getVideoSourceId());
            }
        }
        //根据多个视频id删除多个视频
        if (videoIds.size() > 0) {
            vodClient.deleteBatch(videoIds);
        }

        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        baseMapper.delete(wrapper);
    }
}
